package com.zkj.sentinel;

import com.zkj.sentinel.controller.Demo;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: Test
 * @Description: TODO
 * @Author: Zhukj
 * @Date: 2021-11-5 10:18
 */
public class Test {


    public static void main(String[] args) throws Exception {

        /*long totalMemory = Runtime.getRuntime().totalMemory();
        long maxMemory = Runtime.getRuntime().maxMemory();
        System.out.println("totalMemory：" + totalMemory/1024.0/1024);
        System.out.println("maxMemory：" + maxMemory/1024.0/1024);*/

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("长时间计算开始");
            try {
                TimeUnit.SECONDS.sleep(2L);
                return "计算型任务结束";
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "0.0";
        });
        System.out.println("外层。");
        System.out.println(future.get());
        System.out.println("外层结束");

        CompletableFuture<Integer> ifu = CompletableFuture.supplyAsync(() -> {
            System.out.println("进入2");
            return 0;
        });

        System.out.println("eeee");
        Future<Integer> f = ifu.whenComplete((v, e) -> {
            System.out.println(v);
            System.out.println(e);
        });

        System.out.println(f.get());

    }

    private static void softRefer() {
        Object o1 = new Object();
        SoftReference<Object> s1 = new SoftReference<>(o1);
        System.out.println("gc前："+ o1);
        System.out.println("gc前："+ s1.get());

        List<String> list = new LinkedList<>();
        int i=0;
        do{
            list.add("xxxxxx");
            i++;
        }while (i<360000);

        System.gc();
        System.out.println(o1);
        System.out.println(s1.get());
    }

    private static void strongRefer() {
        Object o1 = new Object();
        Object o2 = o1;

        o1 = null;
        System.gc();
        System.out.println(o1);
        System.out.println(o2);
    }

}
